﻿Imports System.Collections.ObjectModel

''' <summary>
''' This is the base list
''' </summary>
''' <remarks></remarks>
''' <editHistory></editHistory>
Public Class ListBase(Of T As ItemBase)
    Inherits ObservableCollection(Of T)

    Private _isDirty As Boolean

    ''' <summary>
    ''' Gets a value indicating wether this list is dirty.
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public Overridable ReadOnly Property IsDirty() As Boolean
        Get
            If _isDirty Then
                Return True
            Else

                'Look at the items in the list
                Return Me.Any(Function(item) item.IsDirty)

            End If
        End Get
    End Property

    ''' <summary>
    ''' Marks the list and all members of the list as clean
    ''' </summary>
    ''' <remarks></remarks>
    Friend Overridable Sub MarkClean()
        _isDirty = False

        For Each Item As T In Me
            Item.MarkClean()
        Next
    End Sub

    ''' <summary>
    ''' Marks the list as dirty
    ''' </summary>
    ''' <remarks></remarks>
    Friend Sub MarkDirty()
        _isDirty = True
    End Sub

    ''' <summary>
    ''' This marks the list as dirty whenever the list changes.
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnCollectionChanged(ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)

        MarkDirty()

        MyBase.OnCollectionChanged(e)
    End Sub

End Class
